package com.example.demo.aspects;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggingAspect {


//    @Pointcut("@annotation(Loggable)")
//    public void loggableMethods() {
//        // Pointcut expression for methods annotated with @Loggable
//    }

    @Pointcut("@annotation(com.example.demo.annotation.Loggable)")
    public void annotationPoint(){}

    @Pointcut("execution(* com.example.demo.service.DogService.selectByID(..))")
    public void methodPoint(){}



    @Around("annotationPoint()")
    public Object aroundLogByAnno(ProceedingJoinPoint pj) throws Throwable {
        long start=System.currentTimeMillis();
        pj.proceed();
        long end=System.currentTimeMillis();
        System.out.println(String.format("method used time:%d by annotation",end-start));
        return null;
    }

    @Around("methodPoint()")
    public Object aroundLogByMethod(ProceedingJoinPoint pj) throws Throwable {
        long start=System.currentTimeMillis();
        Object result=pj.proceed();
        long end=System.currentTimeMillis();
        System.out.println(String.format("method used time:%d by interface",end-start));
        return result;
    }
}
